package com.siblings.loan.ui.launch.impl

import android.content.Context
import com.siblings.lib_permission.XXPermissions
import com.siblings.loan.utils.helper.SibAnalysisHelper
import com.siblings.loan.http.IHttpCallback
import com.siblings.loan.ui.launch.view.ISibLaunchView
import com.siblings.loan.utils.helper.SharedHelper
import com.siblings.mvp.presenter.MvpPresenter
import org.json.JSONObject

/**
 * Siblings
 * @Author wwdeng
 * @CreateTime: 2024/7/4 15:46
 * @Annotation：启动页 Presenter 实现
 */
class SibLaunchPresenterImpl(context: Context?) : MvpPresenter<ISibLaunchView>(context) {

    private var launchModel: SibLaunchModelImpl? = null
    init {
        launchModel = SibLaunchModelImpl()
    }

    /**
     * 获取地区转发
     */
    fun loadSibTimeZoneAction(){
        mvpView!!.showSibLoading()
        launchModel!!.loadSibTimeZoneAction(object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                when(data.optInt("code")) {
                    0 -> {//成功
                        val dataString = data.optString("data")
                        val timeZone = dataString.split(",")[0]
                        val zoneCode = dataString.split(",")[1]
                        mvpView.showSibTimeZoneRequestData(timeZone,zoneCode)
                    }
                    else -> {
                        val msg = data.optString("msg")
                        mvpView!!.showSibToast(msg)
                        mvpView.hideSibLoading()
                    }
                }
            }
            override fun onRequestError(error: JSONObject) {
                mvpView.showSibToast(error.optString("errorMsg"))
                mvpView.hideSibLoading()
            }
            override fun onRequestFinish() {
            }
        })
    }

    /**
     * 获取App统一配置信息
     */
    fun loadSibCommonConfigAction(){
        launchModel!!.loadSibCommonConfigAction(object : IHttpCallback{
            override fun onRequestSuccess(data: JSONObject) {
                when(data.optInt("code")) {
                    0 -> {//成功
                        mvpView.hideSibLoading()
                        val obj = data.optJSONObject("obj")
                        //保存所有数据
                        if (obj != null) {
                            SharedHelper.saveSibSPData(SharedHelper.SIB_SPKEY.SIBLINGS_COMMON,obj.toString())
                        }
                        val customizeObj = obj?.optJSONObject("customize")
                        val permissionsObj = customizeObj?.optJSONObject("permissions")
                        val permissionArray = permissionsObj?.optJSONArray("permission")

                        if (permissionArray != null) {
                            if (permissionArray.length() > 0){
                                //是否全部申请
                                var isAll = true
                                for (i in 0..<permissionArray.length()){
                                    val permission = permissionArray.optString(i)
                                    //存在未授权的权限
                                    if (!XXPermissions.isGranted(context!!, permission)){
                                        isAll = false
                                        break
                                    }
                                }
                                if (isAll){//已全部授权
                                    //已登录 - 进入主页
                                    if (SharedHelper.getSibSPBoolean(SharedHelper.SIB_SPKEY.SIBLINGS_LOGIN)){
                                        //初始化analysis lib 赋值 用户id 和 token
                                        SibAnalysisHelper.initAnalysisLibAtLoginSuccess()
                                        mvpView.showSibMainPage()
                                    } else {//进入登录页
                                        mvpView.showSibLoginPage()
                                    }
                                } else {
                                    //进入权限页
                                    mvpView.showSibPermissionPage()
                                }
                            } else {
                                //进入登录
                                mvpView.showSibLoginPage()
                            }
                        }
                    }

                    else -> {
                        val msg = data.optString("msg")
                        mvpView!!.showSibToast(msg)
                    }
                }
            }

            override fun onRequestError(error: JSONObject) {
                mvpView.showSibToast(error.optString("errorMsg"))
            }

            override fun onRequestFinish() {
                mvpView.hideSibLoading()
            }
        })
    }
}